package com.qyer.lxp.service.order.impl;

import com.qyer.lxp.common.framework.contants.QyerMethodNameContants;
import com.qyer.lxp.common.framework.convert.TransformSession;
import com.qyer.lxp.common.framework.core.BaseSpringService;
import com.qyer.lxp.common.framework.util.QyerUtils;
import com.qyer.lxp.lxdao.order.IBookProvideDao;
import com.qyer.lxp.model.availability.normal.Available;
import com.qyer.lxp.model.availability.parameter.AvailabilityRequest;
import com.qyer.lxp.model.book.normal.Book;
import com.qyer.lxp.model.book.parameter.BookRequest;
import com.qyer.lxp.model.booklimits.normal.*;
import com.qyer.lxp.model.booklimits.parameter.BookLimitRequest;
import com.qyer.lxp.model.orderstatus.normal.OrderStatus;
import com.qyer.lxp.model.orderstatus.normal.OrderStatuses;
import com.qyer.lxp.model.orderstatus.parameter.OrderStatusRequest;
import com.qyer.lxp.model.product.normal.PickDropModel;
import com.qyer.lxp.model.product.normal.SaleItemDetail;
import com.qyer.lxp.service.order.IBookProvideService;
import com.qyer.lxp.service.product.IProductService;
import net.sf.json.JSONArray;
import org.springframework.stereotype.Service;
import net.sf.json.JSONObject;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *预订供应实现类
 * Created by wangzhen on 16/6/23.
 */
@Service
public class BookProvideServiceImpl extends BaseSpringService implements IBookProvideService {

    @Resource
    private IBookProvideDao bookProvideDao;

    @Resource
    private IProductService productService;

    /**
     * 验价接口
     * 功能说明：传入购买的销售项目code，以及购买的规格或附加选项的code和数量，
     * 获取相应的可售信息和最新的准确价格信息
     *
     * @param availabilityRequest 需要验价参数的封装对象
     * @return 返回验价结果
     * @throws Exception
     */
    public Available getAvailablity(AvailabilityRequest availabilityRequest) {
        if(null == availabilityRequest){
            return null;
        }
        try {
            return bookProvideDao.getAvailablity(availabilityRequest);
        } catch (Exception e) {
            logger.error("调用力行验价接口异常",e);
        }
        return null;
    }

    /**
     * 根据salecode获取接送酒店信息
     * @param saleCode
     * @return
     */
    public List<PickDropModel> getPickDropInfo(String saleCode){
        SaleItemDetail detail=productService.getSaleItemDetailBySaleCode(saleCode);
        if(null!=detail){
            return productService.getPickDrop(detail.getProductCode());
        }
        return null;

    }

    /**
     * 预定限制接口
     * @param saleCode
     * @return
     */
    public Map<String,Object> getBookLimit(String saleCode) {
        if(QyerUtils.isNotEmpty(saleCode)){
            BookLimitRequest bookLimitRequest=new BookLimitRequest();
            List<String> list=new ArrayList<String>();
            list.add(saleCode);
            bookLimitRequest.setSaleCode(list);
            try {
                List<BookLimit> listLimit= bookProvideDao.getBookLimits(bookLimitRequest);
                Map<String,Object> mapAll=new HashMap<String, Object>();
                for(BookLimit book:listLimit){
                    Map<String,Object> map=new HashMap<String,Object>();
                    if(null!=book){

//                        book.setDropoffOnDemand("true");
//                        book.setPickup("true");
//                        book.setPickupOnDemand("true");

                        /*********************************user**********************************/
                        boolean userTrue=false;
                        if(null!=book.getTravelerLimits()){
                            Map<String,Object> mapUser=new HashMap<String, Object>();
                            Map<String,Object> mapUserContent=new HashMap<String, Object>();
                            TravelerLimit limit=book.getTravelerLimits();

//                            limit.setNameLanguageAll("true");
//                            limit.setNeedAllTraveler("true");
//                            limit.setNeedBirthday("true");
//                            limit.setNeedDepartTel("true");
//                            limit.setNeedGender("true");
//                            limit.setNeedIdentityExpireDate("true");
//                            limit.setNeedLeader("true");
//                            limit.setNeedNationality("true");
//                            limit.setNeedTravelerInfo("true");
//                            limit.setNeedWeixin("true");
//                            IdentityType id=new IdentityType();
//                            id.setIdentityCode("passport");
//                            id.setIdentityName("护照");
//                            List<IdentityType> list_id=new ArrayList<IdentityType>();
//                            list_id.add(id);
//                            limit.setIdentityTypes(list_id);

                            int is_multiple=0;//need-all-traveler { false : 0, true : 1 }
                            if("true".equals(limit.getNeedAllTraveler())){
                                is_multiple=1;
                            }
                            mapUser.put("is_multiple",is_multiple);
                            if("true".equals(limit.getNeedTravelerInfo())){
                                Map<String,Object> mapcontent=new HashMap<String, Object>();

                                List<NameLanguages> lan=limit.getNameLanguages();
                                List<Object> listDefault=new ArrayList<Object>();
                                for(NameLanguages languages:lan){
                                    if(languages.getLanguageName().equals("英文")){
                                        Map<String,Object> mapkeyFName=new HashMap<String, Object>();
                                        mapkeyFName.put("field_key","firstname_en");
                                        mapkeyFName.put("admin_config_data",new ArrayList<Map<String,String>>());
                                        Map<String,Object> mapkeyLName=new HashMap<String, Object>();
                                        mapkeyLName.put("field_key","lastname_en");
                                        mapkeyLName.put("admin_config_data",new ArrayList<Map<String,String>>());
                                        listDefault.add(mapkeyFName);
                                        listDefault.add(mapkeyLName);
                                    }else if(languages.getLanguageName().equals("中文")){
                                        Map<String,Object> mapkeyFName=new HashMap<String, Object>();
                                        mapkeyFName.put("field_key","firstname");
                                        mapkeyFName.put("admin_config_data",new ArrayList<Map<String,String>>());
                                        Map<String,Object> mapkeyLName=new HashMap<String, Object>();
                                        mapkeyLName.put("field_key","lastname");
                                        mapkeyLName.put("admin_config_data",new ArrayList<Map<String,String>>());
                                        listDefault.add(mapkeyFName);
                                        listDefault.add(mapkeyLName);
                                    }
                                }

                                if("true".equals(limit.getNeedBirthday())){//生日
                                    Map<String,String> mapkeybirthday=new HashMap<String, String>();
                                    mapkeybirthday.put("field_key","birthday");
                                    listDefault.add(mapkeybirthday);
                                }
                                if("true".equals(limit.getNeedGender())){//性别
                                    Map<String,String> mapkeyGender=new HashMap<String, String>();
                                    mapkeyGender.put("field_key","sex");
                                    listDefault.add(mapkeyGender);
                                }
                                if("true".equals(limit.getNeedNationality())){//国籍
                                    Map<String,String> mapkeyNationality=new HashMap<String, String>();
                                    mapkeyNationality.put("field_key","nationnality");
                                    listDefault.add(mapkeyNationality);
                                }
                                if("true".equals(limit.getNeedIdentityExpireDate())){//证件有效时间
                                    Map<String,String> mapkeyIdentityExpireDate=new HashMap<String, String>();
                                    mapkeyIdentityExpireDate.put("field_key","identity_validity");
                                    listDefault.add(mapkeyIdentityExpireDate);
                                }
                                mapUserContent.put("default", listDefault);


                                //证件信息
                                List<Custom> listCustom=new ArrayList<Custom>();
                                if(null!=limit.getIdentityTypes()&&limit.getIdentityTypes().size()>0){
                                    List<IdentityType> listIdentityType=limit.getIdentityTypes();
                                    List<Map<String,String>> admin_config_data=new ArrayList<Map<String,String>>();
                                    for(IdentityType identityType:listIdentityType){
                                        Map<String,String> mapIdentityType=new HashMap<String, String>();
                                        mapIdentityType.put("key",identityType.getIdentityCode());
                                        mapIdentityType.put("value",identityType.getIdentityName());
                                        admin_config_data.add(mapIdentityType);
                                    }
                                    Custom custom=new Custom();
                                    custom.setField_key("custom_identity_type");
                                    custom.setField_name("证件类型");
                                    custom.setFe_type("select");
                                    custom.setAdmin_config_data(admin_config_data);

                                    Custom customNum=new Custom();
                                    customNum.setField_key("custom_identity_id");
                                    customNum.setField_name("证件号码");
                                    customNum.setFe_type("input");
                                    customNum.setAdmin_config_data(new ArrayList<Map<String,String>>());

                                    listCustom.add(custom);
                                    listCustom.add(customNum);

                                }
                                if("true".equals(limit.getNeedLeader())){
                                    Custom custom=new Custom();
                                    custom.setField_key("custom_leader_firstName");
                                    custom.setField_name("领队姓");
                                    custom.setFe_type("input");
                                    custom.setAdmin_config_data(new ArrayList<Map<String,String>>());

                                    Custom custom1=new Custom();
                                    custom1.setField_key("custom_leader_lastName");
                                    custom1.setField_name("领队名");
                                    custom1.setFe_type("input");
                                    custom1.setAdmin_config_data(new ArrayList<Map<String,String>>());

                                    Custom custom2=new Custom();
                                    custom2.setField_key("custom_leader_phone");
                                    custom2.setField_name("领队电话");
                                    custom2.setFe_type("input");
                                    custom2.setAdmin_config_data(new ArrayList<Map<String,String>>());

                                    listCustom.add(custom);
                                    listCustom.add(custom1);
                                    listCustom.add(custom2);
                                }
                                if(null!=listCustom&&listCustom.size()>0){
                                    mapUserContent.put("custom",listCustom);
                                }
                                userTrue=true;
                            }
                            mapUser.put("content",mapUserContent);
                            if(userTrue){
                                map.put("user",mapUser);
                            }

                        }

                        /*********************************other**********************************/
                        boolean otherTrue=false;
                        Map<String,Object> mapOther=new HashMap<String, Object>();
                        Map<String,Object> mapOtherContent=new HashMap<String, Object>();
                        mapOther.put("is_multiple",0);
                        List<Object> listCustom=new ArrayList<Object>();
                        if(null!=book.getTravelerLimits()&&"true".equals(book.getTravelerLimits().getNeedLocalTel())){//当地电话
                            Custom custom=new Custom();
                            custom.setField_key("contact_phone");
                            custom.setField_name("当地电话");
                            custom.setFe_type("input");
                            custom.setAdmin_config_data(new ArrayList<Map<String,String>>());
                            listCustom.add(custom);
                            otherTrue=true;
                        }
                        if(null!=book.getQuestions()&&book.getQuestions().size()>0){
                            List<BookQuestion> listQuestion=book.getQuestions();
                            for(int j=0;j<listQuestion.size();j++){
                                BookQuestion question=listQuestion.get(j);
                                Custom custom=new Custom();
                                custom.setField_key(question.getCode());
                                custom.setField_name(question.getContent());
                                if(null!=question.getBookAnswers()&&question.getBookAnswers().size()>0){
                                    custom.setFe_type("select");
                                    List<Map<String,String>> listMapAnswer=new ArrayList<Map<String, String>>();
                                    List<BookAnswer> listAnswer=question.getBookAnswers();
                                    for(int i=0;i<listAnswer.size();i++){
                                        BookAnswer answer=listAnswer.get(i);
                                        Map<String,String> mapAnswer=new HashMap<String, String>();
                                        mapAnswer.put("key","answer_"+(i+1));
                                        mapAnswer.put("value",answer.getAnswerCode()+"-"+answer.getAnswerContent());
                                        listMapAnswer.add(mapAnswer);
                                    }
                                    custom.setAdmin_config_data(listMapAnswer);
                                }else{
                                    custom.setFe_type("input");
                                    custom.setAdmin_config_data(new ArrayList<Map<String,String>>());
                                }

                                listCustom.add(custom);
                            }
                            otherTrue=true;
                        }

                        if(otherTrue){
                            mapOtherContent.put("custom",listCustom);
                        }

                        mapOther.put("content",mapOtherContent);
                        if(otherTrue){
                            map.put("other",mapOther);
                        }

                        /*********************************contacts**********************************/
                        boolean concatTrue=false;
                        Map<String,Object> mapContacts=new HashMap<String, Object>();
                        Map<String,Object> mapContactsContent=new HashMap<String, Object>();
                        mapContacts.put("is_multiple",0);
                        if(null!=book.getTravelerLimits()){
//                            List<Object> listPhone=new ArrayList<Object>();
//                            if("true".equals(book.getTravelerLimits().getNeedDepartTel())){
//                                Map<String,String> mapDepartTel=new HashMap<String, String>();
//                                mapDepartTel.put("field_key","phone");
//                                listPhone.add(mapDepartTel);
//                                mapContactsContent.put("phone",listPhone);
//                                concatTrue=true;
//                            }
                            List<Object> listWeixin=new ArrayList<Object>();
                            if("true".equals(book.getTravelerLimits().getNeedWeixin())){
                                mapContactsContent.put("wechat_validate_required",1);
                                concatTrue=true;
                            }

                            mapContacts.put("content",mapContactsContent);
                            if(concatTrue){
                                map.put("contacts",mapContacts);
                            }

                        }
                    }

                    /*********************************pickup**********************************/
                    boolean pickupTrue=false;

                    Map<String,Object> mapPickUp=new HashMap<String, Object>();
                    Map<String,Object> mapPickupContent=new HashMap<String, Object>();
                    mapPickUp.put("is_multiple",0);
                    List<Object> listCustom=new ArrayList<Object>();
                    List<PickDropModel> listPick=this.getPickDropInfo(saleCode);
                    if("true".equals(book.getPickup())&&null!=listPick&&listPick.size()>0){
                        List<Map<String,String>> listHotelAddress=new ArrayList<Map<String,String>>();
                        Custom custom=new Custom();
                        custom.setField_key("custom_hotel_address");
                        custom.setField_name("酒店地址");
                        custom.setFe_type("select");

                        if(null!=listPick&&listPick.size()>0){
                            for(int i=0;i<listPick.size();i++){
                                PickDropModel dropModel=listPick.get(i);
                                if(null!=dropModel){
                                    Map<String,String> mapAdd=new HashMap<String, String>();
                                    String time=dropModel.getTime();
                                    if(QyerUtils.isEmpty(time)){
                                        time="";
                                    }
                                    mapAdd.put("key","hotel_"+(i+1));
                                    mapAdd.put("value",dropModel.getCode()+"-"+dropModel.getLocation()+"-"+dropModel.getAddress());
                                    listHotelAddress.add(mapAdd);
                                }

                            }
                        }
                        custom.setAdmin_config_data(listHotelAddress);
                        listCustom.add(custom);
                        pickupTrue=true;
                    }
                    if("true".equals(book.getPickupOnDemand())){//用户自己输入介入地址
                        Custom custom=new Custom();
                        custom.setField_key("custom_pickup_address");
                        custom.setField_name("接人地址");
                        custom.setFe_type("input");
                        custom.setAdmin_config_data(new ArrayList<Map<String,String>>());
                        listCustom.add(custom);
                        pickupTrue=true;

                    }
                    if(null!=listCustom&&listCustom.size()>0){
                        mapPickupContent.put("custom",listCustom);
                        mapPickUp.put("content",mapPickupContent);
                    }
                    if(pickupTrue){
                        map.put("pickup",mapPickUp);
                    }

                    /*********************************sendoff**********************************/
                    boolean sendOffTrue=false;

                    Map<String,Object> mapSendOff=new HashMap<String, Object>();
                    Map<String,Object> mapSendOffContent=new HashMap<String, Object>();
                    mapSendOff.put("is_multiple",0);
                    if("true".equals(book.getDropoffOnDemand())){
                        List<Object> listSendOffCustom=new ArrayList<Object>();
                        Custom custom=new Custom();
                        custom.setField_key("custom_sendoff_address");
                        custom.setField_name("送人地址");
                        custom.setFe_type("input");
                        custom.setAdmin_config_data(new ArrayList<Map<String,String>>());
                        listSendOffCustom.add(custom);
                        mapSendOffContent.put("custom",listSendOffCustom);
                        mapSendOff.put("content",mapSendOffContent);
                        sendOffTrue=true;
                    }
                    if(sendOffTrue){
                        map.put("sendoff",mapSendOff);
                    }

                    mapAll.put("mods",map);
                    System.out.println(JSONArray.fromObject(listLimit));
                    return mapAll;
                }

            } catch (Exception e) {
                e.printStackTrace();
                logger.error("调用力行预定限制接口异常",e);
            }
        }

        return null;
    }


    /**
     * 获取订单状态
     * @param orderStatusRequest
     * @return
     * @throws Exception
     */
    public List<OrderStatus> getOrderStatus(OrderStatusRequest orderStatusRequest) {
        if(null == orderStatusRequest){
            return null;
        }
        try{
            return bookProvideDao.getOrderStatus(orderStatusRequest);
        }catch(Exception e){
            e.printStackTrace();
            logger.error("获取订单状态失败",e);
        }
        return null;
    }

    /**
     * 调用力行预订接口,预订产品生成订单
     *
     * @param bookRequest 预订请求参数
     * @return 返回生成的订单信息
     */
    @Override
    public Book getBook(BookRequest bookRequest) {
        if(null == bookRequest){
            return null;
        }
        try {
            return bookProvideDao.getBook(bookRequest);
        } catch (Exception e) {
            logger.error("调用力行预订接口异常",e);
        }
        return null;
    }
}
